package com.spsoft.vatoutinv.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.controller.BaseController;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.exception.BusinessException;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.StringUtil;
import com.spsoft.vatoutinv.model.dto.vmsfulloutinvoice.VmsFullOutInvoiceDTO;
import com.spsoft.vatoutinv.model.dto.vmsfulloutinvoice.VmsFullOutInvoiceDownloadDTO;
import com.spsoft.vatoutinv.model.dto.vmsfulloutinvoice.VmsFullOutInvoicePageDTO;
import com.spsoft.vatoutinv.model.dto.vmsfulloutinvoice.VmsFullOutInvoiceUploadInvoiceDto;
import com.spsoft.vatoutinv.model.vo.vmsfulloutinvoice.InvoiceDetailVO;
import com.spsoft.vatoutinv.model.vo.vmsfulloutinvoice.VmsFullOutInvoicePageVO;
import com.spsoft.vatoutinv.service.vmsfulloutinvoice.VmsFullOutInvoiceDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 已开发票信息管理 前端控制器
 * </p>
 *
 * @author wangy
 * @since 2023-11-30
 */
@RestController
@Api(tags = {"已开发票信息管理"})
@RequestMapping("/vmsFullOutInvoice")
@RequiredArgsConstructor
@Validated
@Slf4j
public class VmsFullOutInvoiceController extends BaseController {

    public static final String LOG_MSG = "(数电发票版式文件管理控制层)";

    private final VmsFullOutInvoiceDetailsService fullOutInvoiceDetailsService;

    @ApiOperation("已开发票-分页列表")
    @AnonymousAccess(value = "/page", method = RequestMethod.POST, permissionCode = "VMS_INVOICEDINFO")
    @Log(value = "已开发票-分页列表", type = LogActionType.SELECT, menuName = LogMenuEnum.ORDER)
    public Result<IPage<VmsFullOutInvoicePageVO>> page(@RequestBody VmsFullOutInvoicePageDTO dto) {
        log.info("{}已开发票-分页列表，入参：{}", LOG_MSG, JSONObject.toJSONString(dto));
        return fullOutInvoiceDetailsService.page(dto);
    }

    @ApiOperation("已开发票-发票文件下载")
    @Log(value = "已开发票-发票文件下载", type = LogActionType.DOWNLOAD, menuName = LogMenuEnum.ORDER)
    @AnonymousAccess(value = "/fileDownload", method = RequestMethod.POST, permissionCode = "VMS_FULL_OUT_INVOICE_DOWNLOAD")
    public void fileDownload(@RequestBody VmsFullOutInvoiceDownloadDTO dto) throws IOException {
        if (!"pdf".equals(dto.getDownloadType()) && !"ofd".equals(dto.getDownloadType()) && !"xml".equals(dto.getDownloadType())) {
            throw new BusinessException(ResponseEnum.PARAM_VALIDATED_ERROR);
        }
        log.info("{}已开发票-发票文件下载，入参：{}", LOG_MSG, JSONObject.toJSONString(dto));
        fullOutInvoiceDetailsService.fileDownload(dto);
    }

    @ApiOperation("已开发票-上传发票")
    @Log(value = "已开发票-上传发票", type = LogActionType.UPLOAD, menuName = LogMenuEnum.ORDER)
    @AnonymousAccess(value = "/uploadFphm", method = RequestMethod.POST, permissionCode = "VMS_FULL_OUT_INVOICE_UPLOAD")
    public Result<String> uploadFphm(@RequestBody VmsFullOutInvoiceUploadInvoiceDto dto) {
        if (dto.getFphm().isEmpty()) {
            throw new BusinessException(ResponseEnum.PARAM_VALIDATED_ERROR);
        }
        log.info("{}已开发票-上传发票，入参：{}", LOG_MSG, JSONObject.toJSONString(dto));
        return fullOutInvoiceDetailsService.uploadFphm(dto.getFphm());
    }

    @ApiOperation("回滚已开发票")
    @AnonymousAccess(value = "/rollBackInvoice", method = RequestMethod.POST, permissionCode = "VMS_FULL_OUT_INVOICE_ROLL_BACK")
    @Log(value = "回滚已开发票", type = LogActionType.UPDATE, menuName = LogMenuEnum.ORDER)
    public Result<Object> rollBackInvoice(@RequestBody List<VmsFullOutInvoiceDTO> invoiceDTOList) {
        log.info("{}回滚已开发票，入参：{}", LOG_MSG, JSONObject.toJSONString(invoiceDTOList));
        return fullOutInvoiceDetailsService.rollBackInvoice(invoiceDTOList, getUser());
    }

    @ApiOperation("获取已开发票详情-预览")
    @AnonymousAccess(value = "/getInvoiceDetail", method = RequestMethod.POST, permissionCode = "VMS_INVOICEDINFO")
    @Log(value = "获取已开发票详情-预览", type = LogActionType.SELECT, menuName = LogMenuEnum.ORDER)
    public Result<InvoiceDetailVO> getInvoiceDetailByFphm(String fphm) {
        log.info("{}查询已开发票详情-预览，发票号码：{}", LOG_MSG, fphm);
        return fullOutInvoiceDetailsService.detail(fphm);
    }

    @ApiOperation("已开发票-单张发票下载参数加密")
    @Log(value = "已开发票-单张发票下载参数加密", type = LogActionType.DOWNLOAD, menuName = LogMenuEnum.ORDER)
    @AnonymousAccess(value = "/invoiceDownload", method = RequestMethod.GET, permissionCode = "VMS_FULL_OUT_INVOICE_DOWNLOAD")
    public void invoiceDownload(String text, String type) {
        // http://localhost:8080/vmsFullOutInvoice/singleInvoiceDownload?text=加密字符串&type=pdf
        log.info("{}已开发票-单张发票下载参数加密，入参：加密串:{},类型:{}", LOG_MSG, text, type);
        fullOutInvoiceDetailsService.invoiceDownload(text, type);
    }

    @ApiOperation("已开发票-web端开具发票导入")
    @AnonymousAccess(value = "/webImport", method = RequestMethod.POST, permissionCode = "VMS_FULL_OUT_INVOICE_WEBIMPORT")
    @Log(value = "已开发票-web端开具发票导入", type = LogActionType.UPLOAD, menuName = LogMenuEnum.ORDER)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "excelFile", value = "excel文件", dataTypeClass = MultipartFile.class, dataType = "MultipartFile", required = true),
            @ApiImplicitParam(name = "zipFile", value = "zip文件", dataTypeClass = MultipartFile.class, dataType = "MultipartFile", required = true),
            @ApiImplicitParam(name = "confirmFlag", value = "是否确认覆盖标志", dataTypeClass = boolean.class, dataType = "boolean", required = true)
    })
    public Result<Object> webImport(@RequestParam(value = "excelFile") MultipartFile excelFile,
                                    @RequestParam(value = "zipFile") MultipartFile zipFile,
                                    @RequestParam(value = "confirmFlag") boolean confirmFlag) {
        log.info("{}已开发票-web端开具发票导入，是否覆盖确认：{}", LOG_MSG, confirmFlag);
        return fullOutInvoiceDetailsService.webImport(excelFile, zipFile, confirmFlag);
    }

}
